package com.thoughtworks.impersonator.http.replay;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;

import com.thoughtworks.impersonator.http.message.storage.HttpExchangeStorage;

public class ImpersonatorPipelineFactory implements ChannelPipelineFactory {

    private final HttpExchangeStorage storage;

    public ImpersonatorPipelineFactory(HttpExchangeStorage storage) {
        this.storage = storage;
    }

    @Override
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = pipeline();
        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("chunkAggregator", new HttpChunkAggregator(1048576));
        pipeline.addLast("proxy", new ImpersonatorHandler(storage));
        pipeline.addLast("encoder", new HttpResponseEncoder());
        return pipeline;
    }
}
